package cn.highedu.spring03.controller;

import cn.highedu.spring03.entity.User;
import cn.highedu.spring03.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/users")
public class UserController {
    Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserService userService;

    //@GetMapping("/{username}")
    public User getByUsername(@PathVariable String username) {
        User user = userService.getByUsername(username);
        logger.info("查询用户：{}", user);
        return user;
    }

    @GetMapping("/{id}")
    public User get(@PathVariable Integer id) {
        User user = userService.getById(id);
        if (user == null) {
            throw new UserNotFoundException("用户信息未找到");
        }
        return user;
    }
}
